<?php

/**
 * This is the model class for table "{{contact}}".
 *
 * The followings are the available columns in table '{{contact}}':
 * @property integer $id
 * @property string $name
 * @property string $email
 * @property string $company
 * @property string $country
 * @property string $state
 * @property string $phone
 * @property string $role
 * @property string $comment
 * @property string $submit_date
 */
class Contact extends CActiveRecord {

    public $securityCode;
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Contact the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{contact}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, email, company, comment', 'required'),
            array('name, company, country, state, role', 'length', 'max' => 100),
            array('securityCode', 'length', 'max' => 6),
            array('email, phone', 'length', 'max' => 64),
	    array('comment', 'safe'),
	    array('email', 'email'),
            array('securityCode', 'captcha', 'message' => t('MSG_INCORECT_SECURITY_CODE', 'messages')),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, email, company, country, state, phone, role, comment, securityCode', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'id' => 'ID',
	    'name' => 'Name',
	    'email' => 'Email',
	    'company' => 'Company',
	    'country' => 'Country',
	    'state' => 'State',
	    'phone' => 'Phone',
	    'role' => 'Role',
	    'comment' => 'Comment',
	    'submit_date' => 'Submit Date',
	);
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
	// Warning: Please modify the following code to remove attributes that
	// should not be searched.

	$criteria = new CDbCriteria;

	$criteria->compare('id', $this->id);
	$criteria->compare('name', $this->name, true);
	$criteria->compare('email', $this->email, true);
	$criteria->compare('company', $this->company, true);
	$criteria->compare('country', $this->country, true);
	$criteria->compare('state', $this->state, true);
	$criteria->compare('phone', $this->phone, true);
	$criteria->compare('role', $this->role, true);
	$criteria->compare('comment', $this->comment, true);
	$criteria->compare('submit_date', $this->submit_date, true);

	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	));
    }

    function beforeSave() {
	if (parent::beforeSave()) {
	    if ($this->isNewRecord) {
		$this->submit_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE, time());
	    }
	    return true;
	} else {
	    return false;
	}
    }
    
       protected function afterFind() {
        // convert to display format
        $this->submit_date = strtotime($this->submit_date);
        $this->submit_date = date(LGConstant::DATE_FORMAT, $this->submit_date);
        if ($this->submit_date == LGConstant::DATE_DEFAULT)
            $this->submit_date = '';
	
        parent::afterFind();
    }

}